﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Aj.Core;

public partial class Manager_ArticleEdit : AuthPageBase
{
    protected int id;
    protected string cateid;
    protected string title;
    protected string keyWord;
    protected string faceImg;
    protected string content;
    protected string summary;
    protected string top;
    protected string videoUrl;

    protected DataSet ds = null;
    protected DataSet cateds = null;

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        id = QueryParamInt32("id");
        try
        {
            cateds = CustomQuery.Category_GetCategoryByPId(Utility.ArticleCid);

            if (id > 0)
            {
                ds = CustomQuery.Article_GetArticleById(id);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    cateid = ds.Tables[0].Rows[0]["A_Cateid"].ToString();
                    title = ds.Tables[0].Rows[0]["A_Title"].ToString();
                    keyWord = ds.Tables[0].Rows[0]["A_keyWord"].ToString();
                    faceImg = ds.Tables[0].Rows[0]["A_Pic"].ToString();
                    summary = ds.Tables[0].Rows[0]["A_Summary"].ToString();
                    content = ds.Tables[0].Rows[0]["A_Content"].ToString();
                    top = ds.Tables[0].Rows[0]["A_Top"].ToString();
                    videoUrl = ds.Tables[0].Rows[0]["A_VideoUrl"].ToString();
                }
            }
            if (IsPost)
            {
                if (id > 0)   // 更新
                {
                    int rel = CustomQuery.Article_Update(id, FormParamInt32("cateid"), FormParam("title"), FormParam("keyWord"), FormParam("faceImg"), FormParam("summary"), FormParam("content"), FormParam("top"), FormParam("videoUrl"));
                    if (rel > 0)
                    {
                        GenerateIndexXml();
                        Page_GoTo("ArticleList.aspx", true);
                    }
                    else
                    {
                        MessageBox("修改失败！");
                    }
                }
                else          // 新建
                {
                    int rel = CustomQuery.Article_Add(FormParamInt32("cateid"), FormParam("title"), FormParam("keyWord"), FormParam("faceImg"), FormParam("summary"), FormParam("content"), User.Identity.Name, FormParam("top"), FormParam("videoUrl"));
                    if (rel > 0)
                    {
                        // 如果是新发布的新闻
                        GenerateIndexXml();
                        Page_GoTo("ArticleList.aspx", true);
                    }
                    else
                    {
                        MessageBox("新建失败！");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Utility.WriteErrorLog(ex);
        }
    }

    public void GenerateIndexXml()
    {
        // 就初始首页xml
        if (FormParam("top").IndexOf('B') > -1)
        {
            if (Utility.NewsCid == FormParamInt32("cateid"))
            {
                System.Threading.Thread.Sleep(0);
                Utility.InitNewsXml();
            }
            else if (Utility.VodeoCid == FormParamInt32("cateid") && FormParam("faceImg").IsNotNullAndEmpty())
            {
                System.Threading.Thread.Sleep(0);
                Utility.InitNews2Xml();
            }
        }
    }
}